package com.open.launcher.presenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


import com.open.launcher.activity.PlayerActivity;
import com.open.launcher.bean.details.PlayDetailsBean;
import com.open.launcher.callback.RequestCallback;
import com.open.launcher.service.AHservice;
import com.open.launcher.utils.APPLog;
import com.open.launcher.utils.CacheDataPresenter;
import com.open.launcher.utils.Consts;
import com.open.launcher.utils.Paths;
import com.open.library.utils.PreferencesUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

import static android.content.ContentValues.TAG;


/**
 * Created by Shinelon on 2017/9/16.
 */

public class PlayDetailsPresenter {
    private boolean isDisplayed;
    private Callback callbacks;
    public List<PlayDetailsBean> listContent;
    public PlayDetailsBean detailsBean;
    private Context mContext;

    public PlayDetailsPresenter(Context mContext) {
        this.mContext = mContext;
    }

    //加载数据
    public void loadListItem(String url, Callback callback) {
        this.callbacks = callback;
        AHservice service = new AHservice();
        service.playDetailItemCallback(url, new LoadItemCallBack());
    }

    //加载数据
    public void loadDetailContent(String url, Callback callback) {
        if (callbacks == null) {
            callbacks = callback;
        }
        AHservice service = new AHservice();
        service.filmDetailCallback(url, new LoadDetailCallBack());
    }

    public void toPlay() {
        if (detailsBean != null) {

            OkHttpUtils.post().url("http://"+ PreferencesUtils.getString(mContext, Consts.IP_ADDR_KEY)+ Paths.VOD_STATISTIC)
                    .addParams("id",detailsBean.getId()+"")
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.i(TAG, "onError: ");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Log.i(TAG, "onResponse: "+response);
                        }
                    });
            Intent intent = new Intent(mContext, PlayerActivity.class);
            intent.putExtra("data", detailsBean);
            intent.putExtra("type", 1001);
            intent.putExtra("position",0);
            mContext.startActivity(intent);
        }
    }
    public void toCollection(){
//        CacheDataPresenter.getPresenter(mContext).cacheRecord(detailsBean,"collection_data",20);
//        Toast.makeText(mContext,"收藏成功！", Toast.LENGTH_SHORT).show();
    }

    public void onBindDetailData(IPlayDetailsViewHolder viewHolder) {
        viewHolder.setImg_view(detailsBean.getImage());
        viewHolder.setFilm_name(detailsBean.getName());
        viewHolder.setDirector_tv(detailsBean.getDirector());
        viewHolder.setIntroduce_tv(detailsBean.getInfo());
        viewHolder.setTostar_tv(detailsBean.getStaring());
        viewHolder.setYears_tv(detailsBean.getPublish_at());
        viewHolder.setCopyright(detailsBean.getCopyright());
        APPLog.printInfo("detailsBean==>:" +detailsBean.getCopyright());
    }

    public void onBindMainChanelView(MainViewHolder holder, int position) {
        if (position < listContent.size()) {
            PlayDetailsBean detailsBean = listContent.get(position);
            holder.setFilmImg("http://"+ PreferencesUtils.getString(mContext, Consts.IP_ADDR_KEY) + detailsBean.getImage());
            holder.setFilmName(detailsBean.getName());
        }
    }

    public void setDisplayed(boolean displayed) {
        isDisplayed = displayed;
    }

    public Bundle getParams(int position) {
        Bundle bundle = new Bundle();
        PlayDetailsBean playDetailsBean = listContent.get(position);
        bundle.putInt("id", playDetailsBean.getId());
        return bundle;
    }

    public boolean getVodType(int position) {
        PlayDetailsBean playDetailsBean = listContent.get(position);
        if (playDetailsBean.getTotal_episode() == 1) {
            return true;
        }
        return false;
    }

    public boolean getTelType(int position) {
        PlayDetailsBean playDetailsBean = listContent.get(position);
        if (playDetailsBean.getTotal_episode() > 1) {
            return true;
        }
        return false;
    }


    public interface Callback {
        void onError(int code, String msg);

        void onMovieCompleted(PlayDetailsPresenter presenter, int count);

        void onDetailCompleted(PlayDetailsPresenter presenter);

    }

    public interface MainViewHolder {
        void setFilmImg(String img_r);

        void setFilmName(String filmName);
    }

    public interface IPlayDetailsViewHolder {
        void setImg_view(String url);

        void setFilm_name(String text);

        void setDirector_tv(String text);

        void setTostar_tv(String text);

        void setYears_tv(String text);

        void setIntroduce_tv(String text);

        void setCopyright(String copyright);
    }

    public class LoadItemCallBack implements RequestCallback {

        @Override
        public void onLoadComplete(String contentID, int pageNumber, int count, List contents) {
            if (callbacks == null || !isDisplayed) {
                return;
            }
            listContent = contents;
            callbacks.onMovieCompleted(PlayDetailsPresenter.this, count);
        }

        @Override
        public void onLoading(String contentID) {

        }

        @Override
        public void onFailed(String code, String msg) {

        }
    }

    private class LoadDetailCallBack implements RequestCallback {

        @Override
        public void onLoadComplete(String contentID, int pageNumber, int count, List contents) {
            if (contents != null && contents.size() > 0) {
                detailsBean = (PlayDetailsBean) contents.get(0);
                callbacks.onDetailCompleted(PlayDetailsPresenter.this);
            }
        }

        @Override
        public void onLoading(String contentID) {

        }

        @Override
        public void onFailed(String code, String msg) {

        }
    }
}
